package com.cgsd.heyu.ui.info;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.library.adapter.BaseAdapter;
import com.android.library.adapter.ViewHolder;
import com.android.library.utils.BeanUtils;
import com.cgsd.heyu.R;
import com.cgsd.heyu.entity.CashOutEntity;
import com.cgsd.heyu.presenter.home.HomeContract;
import com.cgsd.heyu.presenter.home.HomePresenter;
import com.cgsd.heyu.ui.BaseActivity;
import com.cgsd.heyu.utils.FormatUtil;
import com.cgsd.heyu.widgets.ErrorCustomView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaoxing
 * Description 提现记录
 * CreateData: 2021/1/28
 */
public class CashOutRecordActivity extends BaseActivity<HomeContract.Presenter> implements HomeContract.CashOutRecordView, OnLoadMoreListener {
    private ErrorCustomView mMErrorView;
    private BaseAdapter<CashOutEntity> adapter;
    private List<CashOutEntity> data = new ArrayList<>();
    private SmartRefreshLayout mSmartRefreshLayout;

    public static void actionStart(Activity activity){
        Intent intent = new Intent(activity,CashOutRecordActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_cash_out_record;
    }

    @Override
    protected void initView() {
        new HomePresenter(this);
        mTitleBar = findViewById(R.id.title_bar);
        mMErrorView = findViewById(R.id.mErrorView);
        mSmartRefreshLayout = findViewById(R.id.mSmartRefreshLayout);
        mSmartRefreshLayout.setEnableRefresh(false);
        RecyclerView mRecyclerView = findViewById(R.id.mRecyclerView);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BaseAdapter<CashOutEntity>(this,R.layout.item_cash_out_record,data) {
            @SuppressLint("SetTextI18n")
            @Override
            public void convert(ViewHolder holder, CashOutEntity cashOutEntity, int position) {
                ImageView ivBg = holder.getView(R.id.iv_pay_way);
                TextView tvIntegral = holder.getView(R.id.tv_integral_num);
                TextView tvStatusText = holder.getView(R.id.tv_status_text);
                holder.setText(R.id.tv_time,cashOutEntity.getCreateDate());
                holder.setText(R.id.tv_title,"提现"+ FormatUtil.getPrice(cashOutEntity.getWithdrawAmount())+"元");
                if (CashOutEntity.IN_REVIEW.equals(cashOutEntity.getWithdrawStatus())){
                    tvStatusText.setText("审核中");
                    tvStatusText.setTextColor(Color.parseColor("#FF8833"));
                    tvIntegral.setText("-"+cashOutEntity.getFlowerNum());
                }else if (CashOutEntity.SUCCESS.equals(cashOutEntity.getWithdrawStatus())){
                    tvStatusText.setText("提现成功");
                    tvStatusText.setTextColor(Color.parseColor("#00BFAC"));
                    tvIntegral.setText("-"+cashOutEntity.getFlowerNum());
                }else {
                    tvStatusText.setText("提现失败，查看原因");
                    tvStatusText.setTextColor(Color.parseColor("#FF3300"));
                    tvIntegral.setText("+"+cashOutEntity.getFlowerNum());
                    tvStatusText.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (TextUtils.isEmpty(data.get(position).getRefusalReason())) {
                                Toast.makeText(mContext, "积分退回", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(mContext, "积分退回：" + data.get(position).getRefusalReason(), Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
                if (CashOutActivity.CASH_OUT_WEIXIN.equals(cashOutEntity.getWithdrawType())){
                    ivBg.setImageResource(R.mipmap.ic_pay_weipay);
                }else {
                    ivBg.setImageResource(R.mipmap.ic_pay_alipay);
                }
            }
        };
        mRecyclerView.setAdapter(adapter);

        mSmartRefreshLayout.setOnLoadMoreListener(this);
    }

    @Override
    protected void initData() {
        mPresenter.cashOutRecord(page);
    }

    @Override
    public void cashOutList(List<CashOutEntity> entities) {
        mSmartRefreshLayout.finishLoadMore();
        if (BeanUtils.isNotEmpty(entities)){
            int index = data.size();
            data.addAll(entities);
            adapter.notifyItemRangeChanged(index,entities.size());
            mSmartRefreshLayout.setEnableLoadMore(true);
        }else {
            mSmartRefreshLayout.setEnableLoadMore(false);
        }
        if (data.size() == 0){
            showErrorView();
        }else {
            mMErrorView.setVisibility(View.GONE);
        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page++;
        mPresenter.cashOutRecord(page);
    }

    /**
     * 数据为null
     */
    private void showErrorView(){
        mMErrorView.showNetworkError(R.mipmap.default_no_data,"暂无提现记录～");
        mMErrorView.setVisibility(View.VISIBLE);
    }
}
